package com.welkom.everydate.security;

import java.util.Collection;
import java.util.Iterator;

import org.springframework.security.access.AccessDecisionVoter;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.WebAuthenticationDetails;

public  class IPAddressVoter implements AccessDecisionVoter {
	public static final String IP_PREFIX = "IP_";
	public static final String IP_LOCAL_HOST = "IP_LOCAL_HOST";
	public boolean supports(ConfigAttribute attribute) {
		// TODO Auto-generated method stub
		return attribute.getAttribute() != null  
                && attribute.getAttribute().startsWith(IP_PREFIX);  
	}
	public boolean supports(Class clazz) {
		// TODO Auto-generated method stub
		return true;  
	}
	public int vote(Authentication authentication, Object object,
			Collection attributes) {
		// TODO Auto-generated method stub
		 if (!(authentication.getDetails() instanceof WebAuthenticationDetails)) {  
	            return ACCESS_DENIED;  
	        }  
	        WebAuthenticationDetails details = (WebAuthenticationDetails) authentication  
	                .getDetails();  
	        String address = details.getRemoteAddress();  
	        int result = ACCESS_ABSTAIN;  
	        Iterator iteration=attributes.iterator();
	        while (iteration.hasNext()) {
	        	ConfigAttribute config =(ConfigAttribute) iteration.next();
	            result = ACCESS_DENIED;  
	            if (IP_LOCAL_HOST.equals(config.getAttribute())) {  
	                if (address.equals("127.0.0.1")  
	                        || address.equals("0:0:0:0:0:0:0:1")) {  
	                    return ACCESS_GRANTED;  
	                }  
	            }  
	        }  
	        return result;  
	}
	

}
